using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameLib.GameItem
{
    class ItemManager
    {
        public List<GameItem> Items;
        List<GameItem> waitItems = null;

        public ItemManager()
        {
            Items = new List<GameItem>();
        }

        public void Clear()
        {
            Items.Clear();
        }

        public void CheckPress(Vector2 pos)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].CheckPress(pos))
                {
                    try
                    {
                        Items[i].ItemPress(Items[i], new EventArgs());
                        if (SoundManager.IsSoundOn())
                        {
                            SoundManager.PlayClick();
                        }
                    }
                    catch (System.Exception)
                    {
                    	
                    }
                }
            }
        }

        public void AddItem(GameItem item)
        {
            Items.Add(item);
        }

        public void RemoveItem(GameItem item)
        {
            Items.Remove(item);
        }

        public void DisableItem(int index)
        {
            Items[index].Enable = false;
        }

        public void EnableItem(int index)
        {
            Items[index].Enable = true;
        }

        public void HideAll()
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].FadeOut();
            }
        }

        public void ShowAll()
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].FadeIn();
            }
        }

        public void AddLater(List<GameItem> waitItems)
        {
            this.waitItems = waitItems;
        }

        public void Update(GameTime gametime)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].Update(gametime);
            }

            if (Items.Count == 0 && waitItems.Count != 0)
            {
                for (int i = 0; i < waitItems.Count; i++)
                {
                    Items.Add(waitItems[i]);
                }
                waitItems.Clear();
                ShowAll();
            }
        }

        public void Draw(SpriteBatch sp)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].Draw(sp);
            }
        }
    }
}
